package com.bojin.studio.ubattery;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import java.util.List;
import java.util.Vector;

import com.google.ads.*;

import android.app.ActionBar;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the
     * sections. We use a {@link android.support.v4.app.FragmentPagerAdapter} derivative, which will
     * keep every loaded fragment in memory. If this becomes too memory intensive, it may be best
     * to switch to a {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    SectionsPagerAdapter mSectionsPagerAdapter;
    SharedPreferences sharedPreferences;
    List<Fragment> fragments;
    /**
     * The {@link ViewPager} that will host the section contents.
     */
    ViewPager mViewPager;
   
    Method dataConnSwitchmethod;
    Class telephonyManagerClass;
    Object ITelephonyStub;
    Class ITelephonyClass;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //Log.e("ffff","onCreate");
      
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
        if(sharedPreferences.getBoolean("parent_checkbox_preference", true))
     	{
         	startService(new Intent(MainActivity.this,MainService.class));
     	}
        // Create the adapter that will return a fragment for each of the three primary sections
        // of the app.
        int j=0;
        fragments = new Vector<Fragment>();
        for(int i=0;i<800;i++)
        {
         j=i%4;
         switch(j)
         {
         case 0:
         fragments.add(Fragment.instantiate(this, InformationFragment.class.getName()));
         break;
         case 1:
         fragments.add(Fragment.instantiate(this, AdviceFragment.class.getName()));
         break;
         case 2:
         fragments.add(Fragment.instantiate(this, ControlFragment.class.getName()));
         break;
         case 3:
         //fragments.add(Fragment.instantiate(this, ProcessFragment.class.getName()));
         fragments.add(Fragment.instantiate(this, SettingFragment.class.getName()));
         break;
         }
        }
        this.mSectionsPagerAdapter  = new SectionsPagerAdapter(super.getSupportFragmentManager(), fragments);

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        mViewPager.setCurrentItem(400);
        /*=============================================================================*/
        AdView adView = (AdView)findViewById(R.id.adView);
        adView.loadAd(new AdRequest());  
        /*=============================================================================*/
    }

   /* @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }*/

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        	Dialog dialog=new AlertDialog.Builder(MainActivity.this)
            .setTitle(getResources().getString(R.string.mquit))
            .setMessage(getResources().getString(R.string.mquit1))
            .setPositiveButton(getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {
    			
    			@Override
    			public void onClick(DialogInterface dialog, int which) {
    				// TODO Auto-generated method stub			
    				finish();				
    			}
    		})
             .setNegativeButton(getResources().getString(R.string.cance), new DialogInterface.OnClickListener() {
    			
    			@Override
    			public void onClick(DialogInterface dialog, int which) {				
    				// TODO Auto-generated method stub
    				
    			}
    		}).create();
            dialog.show();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    
    @Override
    public void onPause(){
    	super.onPause();
    	finish();
    	
    }
    
   

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
     * sections of the app.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {
    	private List<Fragment> fragments;
        public SectionsPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }

        @Override
        public Fragment getItem(int i) {
        	return this.fragments.get(i);
        }

        @Override
        public int getCount() {
            return 800;
        }

        @Override
        public CharSequence getPageTitle(int position) {
        	position=position%4;
            switch (position) {
                case 0: return getString(R.string.title_section1).toUpperCase();
                case 1: return getString(R.string.title_section2).toUpperCase();
                case 2: return getString(R.string.title_section3).toUpperCase();
                case 3: return getString(R.string.title_section4).toUpperCase();
            }
            return null;
        }
        
      
    }
    
    

}
